package com.zhang;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class AutoCode {
    public static void main(String[] args) {
        //构建一个代码生成器对象
        AutoGenerator autoCode = new AutoGenerator();

        //1、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");    //获取项目目录
        globalConfig.setOutputDir(projectPath+"/src/main/java");     //输出目录
        globalConfig.setAuthor("张立松");      //作者
        globalConfig.setOpen(false);        //是否打开资源管理器(windows文件夹)
        globalConfig.setFileOverride(false);    //是否覆盖
        globalConfig.setServiceName("%sService");   //去除Service的I前缀
        globalConfig.setIdType(IdType.ID_WORKER);
        globalConfig.setDateType(DateType.ONLY_DATE);
        //globalConfig.setSwagger2(true);
        autoCode.setGlobalConfig(globalConfig); //全局配置丢到生成器

        //2、配置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/vueblog?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        dataSourceConfig.setDbType(DbType.MYSQL);
        autoCode.setDataSource(dataSourceConfig); //数据源配置丢到生成器

        //3、配置包
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.zhang");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setController("controller");
        packageConfig.setService("service");
        autoCode.setPackageInfo(packageConfig);     //包配置丢到生成器

        //4、策略配置，设置要映射的表名
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("m_visit_count");          //要映射的表名
        strategy.setTablePrefix("m_");      //生成文件时去掉数据库表的前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);    //下划线转驼峰命名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);  //自动生成lombok


//        strategy.setLogicDeleteFieldName("deleted");    //逻辑删除功能
//        // 自动填充配置
//        TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
//        TableFill gmtModified = new TableFill("update_time", FieldFill.INSERT_UPDATE);
//        ArrayList<TableFill> tableFills = new ArrayList<>();
//        tableFills.add(gmtCreate);
//        tableFills.add(gmtModified);
//        strategy.setTableFillList(tableFills);
//        // 乐观锁
//        strategy.setVersionFieldName("version");

        strategy.setRestControllerStyle(true);  //驼峰命名
        strategy.setControllerMappingHyphenStyle(true); //controller里的请求变成下划线形式，localhost:8080/hello_id_2
        autoCode.setStrategy(strategy);     //策略配置丢到生成器

        //执行
        autoCode.execute();
    }
}
